package cn.gailvlun.gll.presentation.profile;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityOptionsCompat;
import android.widget.ImageView;

import com.jakewharton.rxbinding2.widget.RxTextView;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.PhotoActivity;
import cn.gailvlun.gll.databinding.ActivityComplainBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.NullSubscriber;
import cn.gailvlun.gll.net.extra.FeedBackReq;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import cn.gailvlun.gll.widget.photopicker.PhotoPicker;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;



public class ComplainActivity extends BaseActivity {

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, ComplainActivity.class);
        context.startActivity(intent);
    }

    private ActivityComplainBinding mBinding;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_complain);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.nbv.setOnMenuClickListener(new NavigationBarView.OnMenuClickListener() {
            @Override
            public void onMenuClick(NavigationBarView nb) {
                uploadIssue();
            }
        });

        mBinding.pp.setOnPhotoClickListener(new PhotoPicker.OnPhotoClickListener() {
            @Override
            public void onPhotoClick(ImageView iv, String file) {
                ActivityOptionsCompat options = ActivityOptionsCompat
                        .makeSceneTransitionAnimation(mContext, iv, getString(cn.gailvlun.gll.chatkit.R.string.shared_image));
                PhotoActivity.openActivity(mContext, file, options.toBundle());
            }
        });

        Observable<CharSequence> obContent = RxTextView.textChanges(mBinding.cet.getEditText());
        obContent.subscribe(new Consumer<CharSequence>() {
            @Override
            public void accept(CharSequence charSequence) throws Exception {
                mBinding.nbv.setMenuEnable(charSequence.length() != 0);
            }
        });
    }

    private void uploadIssue() {
        String ossKey = null;
        if (!mBinding.pp.getFileList().isEmpty()) {
            ossKey = mBinding.pp.getFileList().get(0).getOssKey();
        }
        final Dialog pd = DialogUtil.showIndeterminate(mContext);
        HttpMethods.getExtraService()
                .feedback(new FeedBackReq(mBinding.cet.getText(), ossKey,
                        mBinding.etContact.getText().toString(), "pm"))
                .compose(RxUtil.applyNullScheduler())
                .compose(this.bindToLifecycle())
                .subscribe(new NullSubscriber() {
                    @Override
                    protected void onSuccess() {
                        ToastUtil.showShort("感谢你的反馈！");
                        pd.dismiss();

                        clearInfo();
                    }

                    @Override
                    protected void onFailure(String errMsg, int code) {
                        DialogUtil.showError(mContext, errMsg);
                        pd.dismiss();
                    }
                });
    }

    private void clearInfo() {
        mBinding.cet.getEditText().setText(null);
        mBinding.pp.clear();
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {

    }

    public class Presenter {

    }
}
